package com.wakala.employment.fragment.message;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.leaf.library.StatusBarUtil;
import com.wakala.employment.R;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.core.constants.Constants;
import com.wakala.employment.databinding.FragmentSystemNoticeBinding;
import com.wakala.employment.fragment.other.RealNameAuthFragment;
import com.wakala.employment.http.bean.MessageInfo;
import com.wakala.employment.utils.Utils;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SystemNoticeFragment#} factory method to
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class SystemNoticeFragment extends BaseFragment<FragmentSystemNoticeBinding> implements View.OnClickListener {

    /**
     * 数据列表组件
     */
    SwipeRecyclerView recyclerView;
    /**
     * 数据列表 下拉刷新组件
     */
    SwipeRefreshLayout swipeRefreshLayout;

    private BaseQuickAdapter<MessageInfo, BaseViewHolder>  mAdapter;
    /**
     * 消息列表集合
     */
    private List<MessageInfo> mMessageInfoList;
    @NonNull
    @Override
    protected FragmentSystemNoticeBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentSystemNoticeBinding.inflate(inflater, container, attachToRoot);
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle();
        titleBar.setTitle("系统消息");
        titleBar.setTitleColor(R.color.black);
        titleBar.setLeftTextColor(R.color.black);
        titleBar.setLeftImageDrawable(getResources().getDrawable(R.drawable.icon_arrow_back));
        titleBar.setBackgroundResource(R.color.white);//设置标题栏背景颜色
        StatusBarUtil.setDarkMode(requireActivity());
        requireActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.white));
        return titleBar;
    }

    @Override
    protected void initArgs() {
        super.initArgs();
    }

    @Override
    protected void initViews() {
        mMessageInfoList = new ArrayList<>();
        recyclerView = binding.recyclerView;
        swipeRefreshLayout = binding.swipeRefreshLayout;
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        //mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
       // mRecyclerView.addItemDecoration(new GridSectionAverageGapItemDecoration(10, 10, 20, 15));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        initData();
        recyclerView.setAdapter(mAdapter = new BaseQuickAdapter<MessageInfo, BaseViewHolder>(R.layout.adapter_system_notice_card_view_list_item,mMessageInfoList) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, MessageInfo messageInfo) {
                TextView tv_name = baseViewHolder.getView(R.id.tv_name);
                tv_name.setText(messageInfo.getName());
                TextView tv_msg_time = baseViewHolder.getView(R.id.tv_msg_time);
                tv_msg_time.setText(messageInfo.getMsgTime());
                TextView tv_title = baseViewHolder.getView(R.id.tv_title);
                tv_title.setText(messageInfo.getTitle());
                TextView tv_summary = baseViewHolder.getView(R.id.tv_summary);
                tv_summary.setText(messageInfo.getSummary());
                //动作名称
                baseViewHolder.setText(R.id.tv_action_name,messageInfo.getActionName());
                LinearLayout lay_action_bar = baseViewHolder.getView(R.id.lay_action_bar);
                lay_action_bar.setOnClickListener(view -> {
                    if(messageInfo.getActionType().equals(Constants.MESSAGE_SYSTEM_NOTICE_ACTION_TYPE_FRAGMENT)){
                        openNewPage(messageInfo.getActionUrl());
                    } else if (messageInfo.getActionType().equals(Constants.MESSAGE_SYSTEM_NOTICE_ACTION_TYPE_WEB)) {
                        Utils.goWeb(getContext(),messageInfo.getActionUrl(),messageInfo.getName());
                    }
                });

            }
        });

        swipeRefreshLayout.setColorSchemeColors(0xff0099cc, 0xffff4444, 0xff669900, 0xffaa66cc, 0xffff8800);
        swipeRefreshLayout.setEnabled(false);
    }

    @Override
    public void onClick(View view) {

    }

    private void initData() {
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setType("sys");
        messageInfo.setId(1);
        messageInfo.setName("系统消息");
        messageInfo.setTitle("实名认证");
        messageInfo.setSummary("提醒您尽快进行实名认证,实名认证后简历将有专属认证标志，系统优先推荐实名认证的用户。");
        messageInfo.setMsgNum(1);
        messageInfo.setMsgTime("2024-10-11");
        messageInfo.setActionType(Constants.MESSAGE_SYSTEM_NOTICE_ACTION_TYPE_FRAGMENT);
        messageInfo.setActionName("去认证");
        messageInfo.setActionUrl("RealNameAuthFragment");
        mMessageInfoList.add(messageInfo);
       // mAdapter.notifyDataSetChanged();
    }
}